<?php

namespace app\admin\controller;

use think\Request;
use think\Controller;

class Peizhi extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $request=Request::instance();
        $list=db('peizhi')->find();
        if($this->request->isPost()){
            $data=input('post.');
            $result=db('peizhi')->where('id',$list['id'])->update(['rs'=>$data['rs']]);
            if($result!=false){
                $this->success('修改成功','peizhi/index');
            }else{
                $this->error('修改失败');
            }
        }
        //dump($list);die;
        return view('peizhi/index',['list'=>$list]);
        //return $this->fetch('login/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        if($this->request->isPost()){
            $data=input('post.');
            $data['password']=md5($data['password']);
            $result=db('admin')->insert($data);
            if($result){
                $this->success('添加成功','admin/index');
            }else{
                $this->error('添加失败');
            }
        }
        //dump($list);die;
        return view('admin/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {

    }
    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //$id=$request->param('id');
        $list=db('admin')->find($id);
        if($this->request->isPost()){
            $data=input('post.');
            if(!$data['password']){
                unset($data['password']);
            }else{
                $data['password']=md5($data['password']);
            }
            $result=db('admin')->where(['id'=>$id])
                ->update($data);
            if($result!==false){
                $this->success('修改成功','admin/index');
            }else{
                $this->error('修改失败');
            }
        }
        //dump($list);die;
        return view('admin/update',['list'=>$list]);
    }
    public function uploadPic(){
        $request=Request::instance();
        $file =$request->file('myfile');
        //dump($file);die;
        //移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $path_dir=ROOT_PATH . 'public' . DS . 'uploads/';
            $path_file='active/';
            $info = $file->validate(['size'=>2097152,'ext'=>'jpg,png,gif,jpeg'])->move($path_dir.$path_file);
            if($info){
                $logo_file=$path_file. $info->getSaveName();
                return ['code'=>'0','data'=>$logo_file];
            }else{
                return ['code'=>'10001','data'=>$file->getError()];
                // 上传失败获取错误信息
            }
        }else{
            return [];
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result=db('admin')->delete($id);
        if($result!==false){
            $this->success('删除成功','admin/index');
        }else{
            $this->error('删除失败');
        }
    }
    public function ajaxChange(){
        $data=input('post.');
        //return $data;
        if($data['sx']==1){
            db('admin')->where('id',$data['id'])->setField('is_deny','0');
        }else{
            db('admin')->where('id',$data['id'])->setField('is_deny','1');
        }
    }
    //企业付款

    public function zq()

    {
        //$list=db('peizhi')->find();
        if($this->request->isPost()){
            $data=input('post.');
            $openid=$data['openid'];
            $price=$data['price'];
            import('Wq.WxPayPubHelper', EXTEND_PATH);
            $url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
            $unifiedOrder = new \UnifiedOrder_pub();
            $list=db('user')->where('openid',$openid)->find();
            if($list){
                if($price<=$list['balance']){
                    $order_no= '100000982014111112'.time();
                    $unifiedOrder->setParameter("partner_trade_no", $order_no);//
                    $unifiedOrder->setParameter("openid", $openid);//
                    $unifiedOrder->setParameter("amount", $price*100);//总金额
                    //$timeStamp = time();
                    $unifiedOrder->setParameter("check_name", 'NO_CHECK');
                    $unifiedOrder->setParameter("desc", 'NO_CHECK');
                    $data = $unifiedOrder->getPrepayId3($url);
                    if($data['return_code']=="SUCCESS"){
                        $re=db('qy')->insertGetId(['openid'=>$openid,'price'=>$price,'addtime'=>time(),'order_no'=>$order_no]);
                        if($re){
                            db('user')->where('openid',$openid)->setDec('balance',$price);
                            $this->success('提现成功','peizhi/index');
                        }else{
                            $this->error('提现失败');
                        }
                    }else{
                        $this->error('提现失败');
                    }
                    //dump($data);die;
                }else{
                    $this->error('余额不足');
                }
            }else{
                $this->error('用户信息不存在');
            }
        }else {

        }
    }
}
